define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'fee_order/index' + location.search,
                    del_url: 'fee_order/del',
                    state_url: 'fee_order/state',
                    table: 'fee_order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                search: false,
                showColumns: false,
                showExport: false,
                showToggle: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'order_sn', title: __('Order_sn'), operate: 'LIKE'},
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
                        {field: 'total_amount', title: __('Total_amount'), operate: false},
                        {field: 'guarantee_fee', title: __('Guarantee_fee'), operate: false},
                        {field: 'settle_fee', title: __('Settle_fee'), operate: false},
                        {field: 'device_fee', title: __('Device_fee'), operate: false},
                        {field: 'manage_fee', title: __('Manage_fee'), operate: false},
                        {field: 'deduct_fee', title: __('Deduct_fee'), operate: false},
                        {field: 'is_pay', title: __('Is_pay'), searchList: {"0":__('Is_pay 0'),"1":__('Is_pay 1')}, formatter: Table.api.formatter.normal},
                        {field: 'paytime', title: __('Paytime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'is_refund', title: __('Is_refund'), searchList: {"0":__('Is_refund 0'),"1":__('Is_refund 1')}, formatter: Table.api.formatter.normal},
                        {field: 'refund_amount', title: __('Refund_amount'), operate:false},
                        {field: 'refundtime', title: __('Refundtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'state', title: __('State'), searchList: {"-2": __('State -2'), "-1": __('State -1'), "0": __('State 0'), "1": __('State 1')}, formatter: Table.api.formatter.normal},
                        {field: 'statetime', title: __('Statetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'pass',
                                    text: __('通过'),
                                    title: __('通过'),
                                    classname: 'btn btn-xs btn-success btn-ajax',
                                    url: $.fn.bootstrapTable.defaults.extend.state_url + "?state=1&ids={ids}",
                                    confirm: '确认审核通过吗？',
                                    callback: function (data) {
                                        // Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    success: function (data, ret) {
                                        $(".btn-refresh").trigger("click");
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return row.state === 0;
                                    }
                                },
                                {
                                    name: 'reject',
                                    text: __('拒绝'),
                                    title: __('拒绝'),
                                    classname: 'btn btn-xs btn-danger btn-ajax',
                                    url: $.fn.bootstrapTable.defaults.extend.state_url + "?state=-1&ids={ids}",
                                    confirm: '确认审核拒绝吗？',
                                    callback: function (data) {
                                        // Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    success: function (data, ret) {
                                        $(".btn-refresh").trigger("click");
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return row.state === 0;
                                    }
                                },
                                {
                                    name: 'log',
                                    text: __('扣除记录'),
                                    title: __('保证金扣除记录'),
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    url: "fee_log/index?ids={ids}",
                                    callback: function (data) {
                                        // Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    success: function (data, ret) {
                                        $(".btn-refresh").trigger("click");
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return row.has_log;
                                    }
                                },
                                {
                                    name: 'deduct',
                                    text: __('扣保证金'),
                                    title: __('扣保证金'),
                                    classname: 'btn btn-xs btn-warning btn-dialog',
                                    url: "fee_log/add?ids={ids}",
                                    callback: function (data) {
                                        // Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    success: function (data, ret) {
                                        $(".btn-refresh").trigger("click");
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return row.is_pay === 1 && row.is_refund === 0;
                                    }
                                },
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
